"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VComponent = void 0;
var utils_1 = require("./utils");
var lifecycle_1 = require("./lifecycle");
var shared_1 = require("./shared");
var context_1 = require("./context");
var instance_1 = require("./instance");
var reactive_1 = require("./reactive");
/**
 * 页面入口方法
 * @param setup {ComponentSetup} setup方法
 */
function VComponent(options) {
    var setupFn; // setup函数
    var miniOptions = {}; // 小程序Component原本的参数
    if (utils_1.isFunction(options)) {
        // options为函数，则options为setup函数
        setupFn = options;
    }
    else {
        if (options.setup === undefined) {
            // 如果options没有setup函数，则直接执行小程序Component方法
            return Component(options);
        }
        var setupFunc = options.setup, otherOptions = __rest(options, ["setup"]);
        setupFn = setupFunc;
        miniOptions = otherOptions;
    }
    // lifetimes
    miniOptions.lifetimes = miniOptions.lifetimes || {};
    for (var key in lifecycle_1.ComponentLifecycle) {
        if (!miniOptions.lifetimes[lifecycle_1.ComponentLifecycle[key]]
            && miniOptions[lifecycle_1.ComponentLifecycle[key]]) {
            miniOptions.lifetimes[lifecycle_1.ComponentLifecycle[key]] = miniOptions[lifecycle_1.ComponentLifecycle[key]];
        }
    }
    // onAttached生命周期
    miniOptions.lifetimes[lifecycle_1.ComponentLifecycle.ATTACHED] =
        shared_1.wrapFuns(function () {
            instance_1.setCurrentInstance(this); // 保存Component实例，供给setup中的生命周期使用
            var context = context_1.createContext.call(this); // VComponent创建实例，并绑定Component实例
            var SetupTask = setupFn.call(this, reactive_1.reactive(shared_1.deepToRaw(this.properties)), context); // 执行setup方法
            if (!utils_1.isPlainObject(SetupTask))
                console.error('setup返回格式错误');
            context.setData(SetupTask); // 将setup返回数据插入Component实例中
            instance_1.setCurrentInstance(null); // 清空储存的pComponent实例
        }, lifecycle_1.createLifecycleMethods(lifecycle_1.ComponentLifecycle.ATTACHED, miniOptions.lifetimes));
    // 其他生命周期
    for (var key in lifecycle_1.ComponentLifecycle) {
        switch (lifecycle_1.ComponentLifecycle[key]) {
            case lifecycle_1.ComponentLifecycle.ATTACHED:
                break;
            default:
                miniOptions.lifetimes[lifecycle_1.ComponentLifecycle[key]] =
                    lifecycle_1.createLifecycleMethods(lifecycle_1.ComponentLifecycle[key], miniOptions.lifetimes);
                break;
        }
    }
    return Component(miniOptions);
}
exports.VComponent = VComponent;
